#delimit;

************************************************************;
***DATA CREATION FOR SCHOOL AGE PAPER***********************;
************************************************************;

************************************************************;
***edited sept 2008 to make more efficient******************;
***adds variables we need for RESTAT revision***************;
************************************************************;

clear;
set more off;
version 8.0;
cap log close;
log using create_data, text replace;

set mem 2000000;
set matsize 800;


************PART 1 -- PARENT CHARACTERISTICS*********************;

*****************************************************************;
***FIRST MINIMUM SIZE FILE WITH PARENT CHARACTERISTICS***********;
*****************************************************************;

*****************************************************************;
*FIRST WE EXTRACT INFORMATION FROM POP FILE;
*****************************************************************;

use pid mpid fpid foedselsaar bmonth sex 
 using /data/regdta/pop_1986g2006;
drop if fpid==. & mpid==.;
rename foedselsaar yob;
gen age=2006-yob;
su;

duplicates drop;

*********************************************************************************;

tempfile pars;
save `pars', replace;

****************************************;
*CALCULATING FAMILY SIZE AND BIRTH ORDER;
****************************************;

sort mpid;
by mpid: egen famsize=count(pid);
replace famsize=. if mpid==.;

gen dage=-1*age;
sort mpid dage;

gen childnum=1;
replace childnum=childnum[_n-1]+1 if mpid==mpid[_n-1] & mpid~=.;

drop age dage;

sort pid;

save `pars', replace;

su;

*************************************************************************;
********************CODE TO CREATE TEEN CHILDBEARING VARIABLES***********;
*************************************************************************;

keep if sex==2;
sort pid;
tempfile women;
save `women', replace;
 
**********Matching Parents to Children*******************************;

*register contains information on anyone who appears in the administrative data at any point from 86-06;

use pid mpid foedselsaar bmonth using /data/regdta/pop_1986g2006 if mpid~=.; 
rename pid cpid;
rename mpid pid;
rename foedselsaar cyob;
rename bmonth cbmonth;
su;

sort pid;
merge pid using `women';
tab _merge;
keep if _merge~=1;*drops children whose mother is not in the qob sample;
gen nochild=0;replace nochild=1 if _merge==2;
drop _merge;

duplicates drop;

****age of mother at birth of child;
gen agebirth=(cyob-yob)+(cbmonth-bmonth)/12;
replace agebirth=(cyob-yob) if agebirth==.;

*fixing agebirth to large number for women who have no children;
replace agebirth=100000 if agebirth==.;

save `women', replace;

su;

***calculating age of mother when oldest child was born;
sort pid;
egen ageyoung=min(agebirth), by (pid);
replace ageyoung=. if ageyoung==100000;
su;

sort pid;
collapse  
ageyoung, 
 by (pid);
su;

merge pid using `pars';
tab _merge;
drop _merge;
su;
sort mpid;
save `pars', replace;

************************************************************************;
*MERGE INFORMATION ON NUMBER OF KIDS FROM POP REGISTER;
************************************************************************;

*BEF2002; 
use pid nkids using /data/befdta/bef2002.dta;
replace nkids=. if nkids==0;
rename pid mpid;
rename nkids mnkids;
sort mpid;

merge mpid using `pars';
tab _merge;
keep if _merge==3 | _merge==2;*keeping women for whom nkids is missing;
drop _merge;

su;
sort mpid;
save `pars', replace;

*BEF2001;
use pid nkids using /data/befdta/bef2001.dta;
replace nkids=. if nkids==0;
rename nkids hnkids;
rename pid mpid;
sort mpid;

merge mpid using `pars';
tab _merge;
keep if _merge==3 | _merge==2;*keeping women for whom nkids is missing;
drop _merge;
replace mnkids=hnkids if (mnkids==. & hnkids ~=.);

su;
drop hnkids;
sort mpid;
save `pars', replace;

*BEF2000;
use pid nkids using /data/befdta/bef2000.dta;
replace nkids=. if nkids==0;
rename nkids hnkids;
rename pid mpid;
sort mpid;

merge mpid using `pars';
tab _merge;
keep if _merge==3 | _merge==2;*keeping women for whom nkids is missing;
drop _merge;
replace mnkids=hnkids if (mnkids==. & hnkids ~=.);

su;
drop hnkids;
sort mpid;
save `pars', replace;

*BEF1999;
use pid nkids using /data/befdta/bef1999.dta;
replace nkids=. if nkids==0;
rename nkids hnkids;
rename pid mpid;
sort mpid;

merge mpid using `pars';
tab _merge;
keep if _merge==3 | _merge==2;*keeping women for whom nkids is missing;
drop _merge;
replace mnkids=hnkids if (mnkids==. & hnkids ~=.);

su;
drop hnkids;
sort mpid;
save `pars', replace;

*BEF1998;
use pid nkids using /data/befdta/bef1998.dta;
replace nkids=. if nkids==0;
rename nkids hnkids;
rename pid mpid;
sort mpid;

merge mpid using `pars';
tab _merge;
keep if _merge==3 | _merge==2;*keeping women for whom nkids is missing;
drop _merge;
replace mnkids=hnkids if (mnkids==. & hnkids ~=.);

su;
drop hnkids;
su;

duplicates drop;
su;

sort mpid;
save `pars', replace;

************************************************************************;
*MERGE INFORMATION ON PARENTAL YOB FROM POP REGISTER;
************************************************************************;
*First Mothers;
use pid foedselsaar sex 
 using /data/regdta/pop_1986g2006 if sex==2;
rename pid mpid;
rename foedselsaar myob;
su;
duplicates drop;

sort mpid;
tempfile mstuff;
save `mstuff';

merge mpid using `pars';
tab _merge;
keep if _merge==2 | _merge==3;
drop _merge;
duplicates drop;
su;
drop sex;

sort fpid;
save `pars', replace;

*Now Fathers;
use pid foedselsaar sex 
 using /data/regdta/pop_1986g2006 if sex==1;
rename pid fpid;
rename foedselsaar fyob;
su;
duplicates drop;

sort fpid;
save `mstuff', replace;

merge fpid using `pars';
tab _merge;
keep if _merge==2 | _merge==3;
drop _merge;
duplicates drop;
su;
drop sex;

sort mpid;
save `pars', replace;


************************************************************************;
*MOTHERS EDUCATION;
************************************************************************;

*****next merge with 2006 administrative data;

use pid sex eduy using /data/regdta/adssb2006 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
rename eduy meduy;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
su;
drop _merge;
sort mpid;
save `pars', replace;

*****next merge with 2005 data;
use pid eduy sex using /data/regdta/adssb2005 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
replace meduy=eduy if meduy==.;
su;
drop _merge eduy;
sort mpid;
save `pars', replace;

*****next merge with 2004 data;
use pid eduy sex using /data/regdta/adssb2004 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
replace meduy=eduy if meduy==.;
su;
drop _merge eduy;
sort mpid;
save `pars', replace;

*****next merge with 2003 data;
use pid eduy sex using /data/regdta/adssb2003 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
replace meduy=eduy if meduy==.;
su;
drop _merge eduy;
sort mpid;
save `pars', replace;

*****next merge with 2002 data;
use pid eduy sex using /data/regdta/adssb2002 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
replace meduy=eduy if meduy==.;
su;
drop _merge eduy;
sort mpid;
save `pars', replace;

*****next merge with 2001 data;
use pid eduy sex using /data/regdta/adssb2001 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
replace meduy=eduy if meduy==.;
su;
drop _merge eduy;
sort mpid;
save `pars', replace;

*****next merge with 2000 data;
use pid eduy sex using /data/regdta/adssb2000 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
replace meduy=eduy if meduy==.;
su;
drop _merge eduy;
sort mpid;
save `pars', replace;

*****next merge with 1999 data;
use pid eduy sex using /data/regdta/adssb1999 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
replace meduy=eduy if meduy==.;
su;
drop _merge eduy;
sort mpid;
save `pars', replace;

*****next merge with 1998 data;
use pid eduy sex using /data/regdta/adssb1998 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
replace meduy=eduy if meduy==.;
su;
drop _merge eduy;
sort mpid;
save `pars', replace;

*****next merge with 1997 data;
use pid eduy sex using /data/regdta/adssb1997 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
replace meduy=eduy if meduy==.;
su;
drop _merge eduy;
sort mpid;
save `pars', replace;

*****next merge with 1996 data;
use pid eduy sex using /data/regdta/adssb1996 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
replace meduy=eduy if meduy==.;
su;
drop _merge eduy;
sort mpid;
save `pars', replace;

*****next merge with 1995 data;
use pid eduy sex using /data/regdta/adssb1995 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
replace meduy=eduy if meduy==.;
su;
drop _merge eduy;
sort mpid;
save `pars', replace;

*****next merge with 1994 data;
use pid eduy sex using /data/regdta/adssb1994 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
replace meduy=eduy if meduy==.;
su;
drop _merge eduy;
sort mpid;
save `pars', replace;

*****next merge with 1993 data;
use pid eduy sex using /data/regdta/adssb1993 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
replace meduy=eduy if meduy==.;
su;
drop _merge eduy;
sort mpid;
save `pars', replace;

*****next merge with 1992 data;
use pid eduy sex using /data/regdta/adssb1992 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
replace meduy=eduy if meduy==.;
su;
drop _merge eduy;
sort mpid;
save `pars', replace;

*****next merge with 1991 data;
use pid eduy sex using /data/regdta/adssb1991 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
replace meduy=eduy if meduy==.;
su;
drop _merge eduy;
sort mpid;
save `pars', replace;

*****next merge with 1990 data;
use pid eduy sex using /data/regdta/adssb1990 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
replace meduy=eduy if meduy==.;
su;
drop _merge eduy;
sort mpid;
save `pars', replace;

*****next merge with 1989 data;
use pid eduy sex using /data/regdta/adssb1989 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
replace meduy=eduy if meduy==.;
su;
drop _merge eduy;
sort mpid;
save `pars', replace;

*****next merge with 1988 data;
use pid eduy sex using /data/regdta/adssb1988 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
replace meduy=eduy if meduy==.;
su;
drop _merge eduy;
sort mpid;
save `pars', replace;

*****next merge with 1987 data;
use pid eduy sex using /data/regdta/adssb1987 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
replace meduy=eduy if meduy==.;
su;
drop _merge eduy;
sort mpid;
save `pars', replace;

*****next merge with 1986 data;
use pid eduy sex using /data/regdta/adssb1986 if sex==2;
rename pid mpid;
drop sex;
replace eduy=. if eduy==0;
sort mpid;
merge mpid using `pars';
keep if _merge==2 | _merge==3;
replace meduy=eduy if meduy==.;
su;
drop _merge eduy;
sort mpid;
save `pars', replace;

duplicates drop;

su;
sort pid;
save ~/school_age/restat/parent_data.dta, replace;



************PART 2 -- CHILD CHARACTERISTICS*********************;

*****************************************************************;
***NOW VARIABLES FOR CHILDREN************************************;
*****************************************************************;


*****************************************************************;
*FIRST WE EXTRACT INFORMATION FROM POP FILE;
*****************************************************************;

use pid foedselsaar mpid fpid twinid cob2006
 bmonth reg_stat2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
rename cob2006 cob;

drop if yob>1990;
drop if yob<1962;
drop if cob~=0;

duplicates drop;

su;

sort pid;
tempfile pars;
save `pars', replace;

***now begin a process of merging with the administrative data in order
to get education. If this information is not available in 2006 (maybe because individual is not in the sample that year, 
we look for it in 1999 and so on down to 1986;

*****next merge with 2006 administrative data;

use pid sex eduy pearn hrs unempm using /data/regdta/adssb2006;
replace eduy=. if eduy==0;
gen eduy2006=eduy;***marker for education in 2006 files;
gen pearn106=pearn;drop pearn;
gen hrs106=hrs;drop hrs;
gen unempm106=unempm;drop unempm;
rename eduy heduy;
rename sex hsex;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
su;
drop _merge;
sort pid;
save `pars', replace;

*****next merge with 2005 data;
use pid eduy sex pearn hrs unempm using /data/regdta/adssb2005;
replace eduy=. if eduy==0;
gen eduy2005=eduy;***marker for education in 2005 files;
gen pearn105=pearn;drop pearn;
gen hrs105=hrs;drop hrs;
gen unempm105=unempm;drop unempm;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
replace heduy=eduy if heduy==.;
replace hsex=sex if hsex==.;
su;
drop _merge eduy sex;
sort pid;
save `pars', replace;

*****next merge with 2004 data;
use pid eduy sex pearn hrs unempm using /data/regdta/adssb2004;
replace eduy=. if eduy==0;
gen pearn104=pearn;drop pearn;
gen hrs104=hrs;drop hrs;
gen unempm104=unempm;drop unempm;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
replace heduy=eduy if heduy==.;
replace hsex=sex if hsex==.;
su;
drop _merge eduy sex;
sort pid;
save `pars', replace;

*****next merge with 2003 data;
use pid eduy sex pearn hrs unempm using /data/regdta/adssb2003;
replace eduy=. if eduy==0;
gen pearn103=pearn;drop pearn;
gen hrs103=hrs;drop hrs;
gen unempm103=unempm;drop unempm;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
replace heduy=eduy if heduy==.;
replace hsex=sex if hsex==.;
su;
drop _merge eduy sex;
sort pid;
save `pars', replace;

*****next merge with 2002 data;
use pid eduy sex pearn hrs unempm using /data/regdta/adssb2002;
replace eduy=. if eduy==0;
gen pearn102=pearn;drop pearn;
gen hrs102=hrs;drop hrs;
gen unempm102=unempm;drop unempm;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
replace heduy=eduy if heduy==.;
replace hsex=sex if hsex==.;
su;
drop _merge eduy sex;
sort pid;
save `pars', replace;

*****next merge with 2001 data;
use pid eduy sex pearn hrs unempm using /data/regdta/adssb2001;
replace eduy=. if eduy==0;
gen pearn101=pearn;drop pearn;
gen hrs101=hrs;drop hrs;
gen unempm101=unempm;drop unempm;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
replace heduy=eduy if heduy==.;
replace hsex=sex if hsex==.;
su;
drop _merge eduy sex;
sort pid;
save `pars', replace;

*****next merge with 2000 data;
use pid eduy sex pearn hrs unempm using /data/regdta/adssb2000;
replace eduy=. if eduy==0;
gen pearn100=pearn;drop pearn;
gen hrs100=hrs;drop hrs;
gen unempm100=unempm;drop unempm;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
replace heduy=eduy if heduy==.;
replace hsex=sex if hsex==.;
su;
drop _merge eduy sex;
sort pid;
save `pars', replace;

*****next merge with 1999 data;
use pid eduy sex pearn hrs unempm using /data/regdta/adssb1999;
replace eduy=. if eduy==0;
gen pearn99=pearn;drop pearn;
gen hrs99=hrs;drop hrs;
gen unempm99=unempm;drop unempm;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
replace heduy=eduy if heduy==.;
replace hsex=sex if hsex==.;
su;
drop _merge eduy sex;
sort pid;
save `pars', replace;

*****next merge with 1998 data;
use pid eduy sex pearn hrs unempm using /data/regdta/adssb1998;
replace eduy=. if eduy==0;
gen pearn98=pearn;drop pearn;
gen hrs98=hrs;drop hrs;
gen unempm98=unempm;drop unempm;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
replace heduy=eduy if heduy==.;
replace hsex=sex if hsex==.;
su;
drop _merge eduy sex;
sort pid;
save `pars', replace;

*****next merge with 1997 data;
use pid eduy sex pearn hrs unempm using /data/regdta/adssb1997;
replace eduy=. if eduy==0;
gen pearn97=pearn;drop pearn;
gen hrs97=hrs;drop hrs;
gen unempm97=unempm;drop unempm;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
replace heduy=eduy if heduy==.;
replace hsex=sex if hsex==.;
su;
drop _merge eduy sex;
sort pid;
save `pars', replace;

*****next merge with 1996 data;
use pid eduy sex pearn hrs unempm using /data/regdta/adssb1996;
replace eduy=. if eduy==0;
gen pearn96=pearn;drop pearn;
gen hrs96=hrs;drop hrs;
gen unempm96=unempm;drop unempm;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
replace heduy=eduy if heduy==.;
replace hsex=sex if hsex==.;
su;
drop _merge eduy sex;
sort pid;
save `pars', replace;

*****next merge with 1995 data;
use pid eduy sex pearn hrs unempm using /data/regdta/adssb1995;
replace eduy=. if eduy==0;
gen pearn95=pearn;drop pearn;
gen hrs95=hrs;drop hrs;
gen unempm95=unempm;drop unempm;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
replace heduy=eduy if heduy==.;
replace hsex=sex if hsex==.;
su;
drop _merge eduy sex;
sort pid;
save `pars', replace;

*****next merge with 1994 data;
use pid eduy sex pearn hrs unempm using /data/regdta/adssb1994;
replace eduy=. if eduy==0;
gen pearn94=pearn;drop pearn;
gen hrs94=hrs;drop hrs;
gen unempm94=unempm;drop unempm;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
replace heduy=eduy if heduy==.;
replace hsex=sex if hsex==.;
su;
drop _merge eduy sex;
sort pid;
save `pars', replace;

*****next merge with 1993 data;
use pid eduy sex pearn hrs unempm using /data/regdta/adssb1993;
replace eduy=. if eduy==0;
gen pearn93=pearn;drop pearn;
gen hrs93=hrs;drop hrs;
gen unempm93=unempm;drop unempm;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
replace heduy=eduy if heduy==.;
replace hsex=sex if hsex==.;
su;
drop _merge eduy sex;
sort pid;
save `pars', replace;

*****next merge with 1992 data;
use pid eduy sex pearn hrs unempm using /data/regdta/adssb1992;
replace eduy=. if eduy==0;
gen pearn92=pearn;drop pearn;
gen hrs92=hrs;drop hrs;
gen unempm92=unempm;drop unempm;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
replace heduy=eduy if heduy==.;
replace hsex=sex if hsex==.;
su;
drop _merge eduy sex;
sort pid;
save `pars', replace;

*****next merge with 1991 data;
use pid eduy sex pearn hrs unempm using /data/regdta/adssb1991;
replace eduy=. if eduy==0;
gen pearn91=pearn;drop pearn;
gen hrs91=hrs;drop hrs;
gen unempm91=unempm;drop unempm;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
replace heduy=eduy if heduy==.;
replace hsex=sex if hsex==.;
su;
drop _merge eduy sex;
sort pid;
save `pars', replace;

*****next merge with 1990 data;
use pid eduy sex pearn hrs unempm using /data/regdta/adssb1990;
replace eduy=. if eduy==0;
gen pearn90=pearn;drop pearn;
gen hrs90=hrs;drop hrs;
gen unempm90=unempm;drop unempm;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
replace heduy=eduy if heduy==.;
replace hsex=sex if hsex==.;
su;
drop _merge eduy sex;
sort pid;
save `pars', replace;

*****next merge with 1989 data;
use pid eduy sex pearn hrs unempm using /data/regdta/adssb1989;
replace eduy=. if eduy==0;
gen pearn89=pearn;drop pearn;
gen hrs89=hrs;drop hrs;
gen unempm89=unempm;drop unempm;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
replace heduy=eduy if heduy==.;
replace hsex=sex if hsex==.;
su;
drop _merge eduy sex;
sort pid;
save `pars', replace;

*****next merge with 1988 data;
use pid eduy sex pearn hrs unempm using /data/regdta/adssb1988;
replace eduy=. if eduy==0;
gen pearn88=pearn;drop pearn;
gen hrs88=hrs;drop hrs;
gen unempm88=unempm;drop unempm;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
replace heduy=eduy if heduy==.;
replace hsex=sex if hsex==.;
su;
drop _merge eduy sex;
sort pid;
save `pars', replace;

*****next merge with 1987 data;
use pid eduy sex pearn hrs using /data/regdta/adssb1987;
replace eduy=. if eduy==0;
gen pearn87=pearn;drop pearn;
gen hrs87=hrs;drop hrs;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
replace heduy=eduy if heduy==.;
replace hsex=sex if hsex==.;
su;
drop _merge eduy sex;
sort pid;
save `pars', replace;

*****next merge with 1986 data;
use pid eduy sex pearn hrs using /data/regdta/adssb1986;
replace eduy=. if eduy==0;
gen pearn86=pearn;drop pearn;
gen hrs86=hrs;drop hrs;
sort pid;
merge pid using `pars';
keep if _merge==2 | _merge==3;
replace heduy=eduy if heduy==.;
replace hsex=sex if hsex==.;
su;
drop _merge eduy sex;
sort pid;
save `pars', replace;

keep pid heduy yob twinid hsex mpid fpid
twinid eduy2006 eduy2005 cob bmonth 
pearn86 pearn87 pearn88 pearn89 pearn90 pearn91 pearn92 pearn93 pearn94
pearn95 pearn96 pearn97 pearn98 pearn99 pearn100 pearn101 pearn102
pearn103 pearn104 pearn105 pearn106
hrs86 hrs87 hrs88 hrs89 hrs90 hrs91 hrs92 hrs93 hrs94 hrs95 hrs96 hrs97 hrs98
hrs99 hrs100 hrs101 hrs102 hrs103 hrs104 hrs105 hrs106 
reg_stat2006
unempm88 unempm89 unempm90 unempm91 unempm92 unempm93 unempm94 unempm95
unempm96 unempm97 unempm98 unempm99 unempm100 unempm101 unempm102 unempm103
unempm104 unempm105 unempm106;

su;

gen eduy=heduy;
gen sex=hsex;
su;
gen age=2006-yob;
gen cohort=yob-1900;
tab yob;

drop heduy hsex;

gen female=0 if sex==1;
replace female=1 if sex==2;

su;

keep pid fpid mpid twinid eduy age sex eduy2006 eduy2005 yob cob bmonth 
pearn86 pearn87 pearn88 pearn89 pearn90 pearn91 pearn92 pearn93 pearn94
pearn95 pearn96 pearn97 pearn98 pearn99 pearn100 pearn101 pearn102
pearn103 pearn104 pearn105 pearn106
hrs86 hrs87 hrs88 hrs89 hrs90 hrs91 hrs92 hrs93 hrs94 hrs95 hrs96 hrs97 hrs98
hrs99 hrs100 hrs101 hrs102 hrs103 hrs104 hrs105 hrs106 
unempm88 unempm89 unempm90 unempm91 unempm92 unempm93 unempm94 unempm95
unempm96 unempm97 unempm98 unempm99 unempm100 unempm101 unempm102 unempm103
unempm104 unempm105 unempm106
reg_stat2006;

su;
sort pid;

save `pars', replace;

************MERGE WITH 1967-85 EARNINGS***********************************;
use /data/regdta/pearn6700wide.dta;
su;
rename pearn1967 pearn67;
rename pearn1968 pearn68;
rename pearn1969 pearn69;
rename pearn1970 pearn70;
rename pearn1971 pearn71;
rename pearn1972 pearn72;
rename pearn1973 pearn73;
rename pearn1974 pearn74;
rename pearn1975 pearn75;
rename pearn1976 pearn76;
rename pearn1977 pearn77;
rename pearn1978 pearn78;
rename pearn1979 pearn79;
rename pearn1980 pearn80;
rename pearn1981 pearn81;
rename pearn1982 pearn82;
rename pearn1983 pearn83;
rename pearn1984 pearn84;
rename pearn1985 pearn85;
keep pid pearn76 pearn77 pearn78
pearn79 pearn80 pearn81 pearn82 pearn83 pearn84 pearn85;
sort pid;
merge pid using `pars';
tab _merge;
drop if _merge==1;
drop _merge;
save `pars', replace;

duplicates drop;
su;

sort pid;

save `pars', replace;

*************************************************************************;
**************MERGE WITH MILITARY DATA***********************************;
*************NEW DATA****************************************************;
*************************************************************************;

use /data/regdta/sesjon;
su;

gen yr_test=yr;
gen yob_mil=foedselsaar;
gen sex_mil=sex;
keep yr_test pid hgt wgt health yob_mil sex_mil ability sesjondt healthdt 
vpv mental;
su;
sort pid;
merge pid using `pars';
su;
tab _merge;
keep if _merge==2 | _merge==3;

drop _merge;

duplicates drop;

sort pid;

save `pars', replace;

*********************************************************************************;
*ADDING IN GRADES FOR THE YEARS 2002-2006;
*********************************************************************************;

use /data/utddta/grunnskar0206;
sort pid fagkode avgdt stp skr mun;
drop if pid==pid[_n-1] & fagkode==fagkode[_n-1] & avgdt==avgdt[_n-1] & stp==stp[_n-1] & skr==skr[_n-1] 
& mun==mun[_n-1];
sort pid fagkode avgdt;
drop if (pid==pid[_n-1] & pid~=. & fagkode==fagkode[_n-1] & fagkode~=" ");
keep if fagkode=="MAT" | fagkode=="NAM" | fagkode=="NOM"| fagkode=="NOS";
reshape wide stp skr mun avgdt, i(pid) j(fagkode) string;
sum;

sort pid;

merge pid using `pars';
tab _merge;
keep if _merge==2 | _merge==3;
drop _merge;
su;

duplicates drop;
sort pid;

save `pars', replace;

su;

***************************************************************;
***************SOCIAL ASSISTANCE DATA**************************;
***************************************************************;
use /data/FDdta/tab_shj.dta;
duplicates drop;
rename shjaar year;
replace year=year-1900;
keep mndutbet pid year;
su;
tab mndutbet;
drop if mndutbet==0;

sort pid year;
reshape wide mndutbet, i(pid) j(year);
su;

sort pid;

merge pid using `pars';
tab _merge;
keep if _merge==2 | _merge==3;
drop _merge;
su;

duplicates drop;

su;

*********************************************************************************;
*NOW SAVING;
*********************************************************************************;

drop if age==.;
sort pid;
save ~/school_age/restat/child_data.dta, replace;

su;


*****PART 3 -- SCHOOL STARTING AGE**********************************;


*****************************************************************;
*1960 COHORT;
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1960;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1974.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1974-7 if edut00<200000;
replace schoolstart=1974-9 if edut00>=200000;
gen predschoolstart=1960+7;
tab predschoolstart;
tab bmonth;
tempfile co60;
sort pid;
save `co60', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1960;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1975.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1975-7 if edut00<200000;
replace schoolstart=1975-9 if edut00>=200000;
gen predschoolstart=1960+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co60';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co60', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1960;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1976.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1976-7 if edut00<200000;
replace schoolstart=1976-9 if edut00>=200000;
gen predschoolstart=1960+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co60';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co60', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1960;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1977.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1977-7 if edut00<200000;
replace schoolstart=1977-9 if edut00>=200000;
gen predschoolstart=1960+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co60';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co60', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1960;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1978.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1978-7 if edut00<200000;
replace schoolstart=1978-9 if edut00>=200000;
gen predschoolstart=1960+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co60';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co60', replace;

*****************************************************************;
*1961 COHORT;
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1961;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1975.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1975-7 if edut00<200000;
replace schoolstart=1975-9 if edut00>=200000;
gen predschoolstart=1961+7;
tab predschoolstart;
tab bmonth;
tempfile co61;
sort pid;
save `co61', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1961;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1976.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1976-7 if edut00<200000;
replace schoolstart=1976-9 if edut00>=200000;
gen predschoolstart=1961+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co61';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co61', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1961;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1977.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1977-7 if edut00<200000;
replace schoolstart=1977-9 if edut00>=200000;
gen predschoolstart=1961+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co61';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co61', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1961;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1978.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1978-7 if edut00<200000;
replace schoolstart=1978-9 if edut00>=200000;
gen predschoolstart=1961+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co61';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co61', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1961;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1979.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1979-7 if edut00<200000;
replace schoolstart=1979-9 if edut00>=200000;
gen predschoolstart=1961+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co61';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co61', replace;
*****************************************************************;
*1962 COHORT
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1962;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1976.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1976-7 if edut00<200000;
replace schoolstart=1976-9 if edut00>=200000;
gen predschoolstart=1962+7;
tab predschoolstart;
tab bmonth;
tempfile co62;
sort pid;
save `co62', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1962;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1977.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1977-7 if edut00<200000;
replace schoolstart=1977-9 if edut00>=200000;
gen predschoolstart=1962+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co62';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co62', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1962;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1978.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1978-7 if edut00<200000;
replace schoolstart=1978-9 if edut00>=200000;
gen predschoolstart=1962+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co62';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co62', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1962;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1979.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1979-7 if edut00<200000;
replace schoolstart=1979-9 if edut00>=200000;
gen predschoolstart=1962+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co62';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co62', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1962;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1980.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1980-7 if edut00<200000;
replace schoolstart=1980-9 if edut00>=200000;
gen predschoolstart=1962+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co62';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co62', replace;



*****************************************************************;
*1963 COHORT
*****************************************************************;



use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1963;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1977.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1977-7 if edut00<200000;
replace schoolstart=1977-9 if edut00>=200000;
gen predschoolstart=1963+7;
tab predschoolstart;
tab bmonth;
tempfile co63;
sort pid;
save `co63', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1963;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1978.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1978-7 if edut00<200000;
replace schoolstart=1978-9 if edut00>=200000;
gen predschoolstart=1963+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co63';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co63', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1963;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1979.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1979-7 if edut00<200000;
replace schoolstart=1979-9 if edut00>=200000;
gen predschoolstart=1963+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co63';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co63', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1963;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1980.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1980-7 if edut00<200000;
replace schoolstart=1980-9 if edut00>=200000;
gen predschoolstart=1963+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co63';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co63', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1963;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1981.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1981-7 if edut00<200000;
replace schoolstart=1981-9 if edut00>=200000;
gen predschoolstart=1963+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co63';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co63', replace;


*****************************************************************;
*1964 COHORT
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1964;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1978.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1978-7 if edut00<200000;
replace schoolstart=1978-9 if edut00>=200000;
gen predschoolstart=1964+7;
tab predschoolstart;
tab bmonth;
tempfile co64;
sort pid;
save `co64', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1964;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1979.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1979-7 if edut00<200000;
replace schoolstart=1979-9 if edut00>=200000;
gen predschoolstart=1964+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co64';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co64', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1964;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1980.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1980-7 if edut00<200000;
replace schoolstart=1980-9 if edut00>=200000;
gen predschoolstart=1964+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co64';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co64', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1964;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1981.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1981-7 if edut00<200000;
replace schoolstart=1981-9 if edut00>=200000;
gen predschoolstart=1964+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co64';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co64', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1964;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1982.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1982-7 if edut00<200000;
replace schoolstart=1982-9 if edut00>=200000;
gen predschoolstart=1964+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co64';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co64', replace;


*****************************************************************;
*1965 COHORT
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1965;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1979.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1979-7 if edut00<200000;
replace schoolstart=1979-9 if edut00>=200000;
gen predschoolstart=1965+7;
tab predschoolstart;
tab bmonth;
tempfile co65;
sort pid;
save `co65', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1965;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1980.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1980-7 if edut00<200000;
replace schoolstart=1980-9 if edut00>=200000;
gen predschoolstart=1965+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co65';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co65', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1965;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1981.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1981-7 if edut00<200000;
replace schoolstart=1981-9 if edut00>=200000;
gen predschoolstart=1965+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co65';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co65', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1965;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1982.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1982-7 if edut00<200000;
replace schoolstart=1982-9 if edut00>=200000;
gen predschoolstart=1965+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co65';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co65', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1965;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1983.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1983-7 if edut00<200000;
replace schoolstart=1983-9 if edut00>=200000;
gen predschoolstart=1965+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co65';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co65', replace;



*****************************************************************;
*1966 COHORT
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1966;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1980.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1980-7 if edut00<200000;
replace schoolstart=1980-9 if edut00>=200000;
gen predschoolstart=1966+7;
tab predschoolstart;
tab bmonth;
tempfile co66;
sort pid;
save `co66', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1966;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1981.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1981-7 if edut00<200000;
replace schoolstart=1981-9 if edut00>=200000;
gen predschoolstart=1966+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co66';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co66', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1966;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1982.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1982-7 if edut00<200000;
replace schoolstart=1982-9 if edut00>=200000;
gen predschoolstart=1966+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co66';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co66', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1966;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1983.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1983-7 if edut00<200000;
replace schoolstart=1983-9 if edut00>=200000;
gen predschoolstart=1966+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co66';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co66', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1966;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1984.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1984-7 if edut00<200000;
replace schoolstart=1984-9 if edut00>=200000;
gen predschoolstart=1966+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co66';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co66', replace;


*****************************************************************;
*1967 COHORT
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1967;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1981.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1981-7 if edut00<200000;
replace schoolstart=1981-9 if edut00>=200000;
gen predschoolstart=1967+7;
tab predschoolstart;
tab bmonth;
tempfile co67;
sort pid;
save `co67', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1967;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1982.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1982-7 if edut00<200000;
replace schoolstart=1982-9 if edut00>=200000;
gen predschoolstart=1967+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co67';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co67', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1967;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1983.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1983-7 if edut00<200000;
replace schoolstart=1983-9 if edut00>=200000;
gen predschoolstart=1967+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co67';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co67', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1967;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1984.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1984-7 if edut00<200000;
replace schoolstart=1984-9 if edut00>=200000;
gen predschoolstart=1967+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co67';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co67', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1967;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1985.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1985-7 if edut00<200000;
replace schoolstart=1985-9 if edut00>=200000;
gen predschoolstart=1967+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co67';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co67', replace;

*****************************************************************;
*1968 COHORT
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1968;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1982.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1982-7 if edut00<200000;
replace schoolstart=1982-9 if edut00>=200000;
gen predschoolstart=1968+7;
tab predschoolstart;
tab bmonth;
tempfile co68;
sort pid;
save `co68', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1968;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1983.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1983-7 if edut00<200000;
replace schoolstart=1983-9 if edut00>=200000;
gen predschoolstart=1968+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co68';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co68', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1968;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1984.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1984-7 if edut00<200000;
replace schoolstart=1984-9 if edut00>=200000;
gen predschoolstart=1968+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co68';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co68', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1968;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1985.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1985-7 if edut00<200000;
replace schoolstart=1985-9 if edut00>=200000;
gen predschoolstart=1968+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co68';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co68', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1968;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1986.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1986-7 if edut00<200000;
replace schoolstart=1986-9 if edut00>=200000;
gen predschoolstart=1968+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co68';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co68', replace;
*****************************************************************;
*1969 COHORT
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1969;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1983.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1983-7 if edut00<200000;
replace schoolstart=1983-9 if edut00>=200000;
gen predschoolstart=1969+7;
tab predschoolstart;
tab bmonth;
tempfile co69;
sort pid;
save `co69', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1969;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1984.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1984-7 if edut00<200000;
replace schoolstart=1984-9 if edut00>=200000;
gen predschoolstart=1969+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co69';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co69', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1969;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1985.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1985-7 if edut00<200000;
replace schoolstart=1985-9 if edut00>=200000;
gen predschoolstart=1969+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co69';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co69', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1969;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1986.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1986-7 if edut00<200000;
replace schoolstart=1986-9 if edut00>=200000;
gen predschoolstart=1969+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co69';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co69', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1969;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1987.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1987-7 if edut00<200000;
replace schoolstart=1987-9 if edut00>=200000;
gen predschoolstart=1969+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co69';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co69', replace;

***********************************;
*COHORT 1970;
***********************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1970;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1984.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1984-7 if edut00<200000;
replace schoolstart=1984-9 if edut00>=200000;
gen predschoolstart=1970+7;
tab predschoolstart;
tab bmonth;
tempfile co70;
sort pid;
save `co70', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1970;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1985.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1985-7 if edut00<200000;
replace schoolstart=1985-9 if edut00>=200000;
gen predschoolstart=1970+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co70';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co70', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1970;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1986.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1986-7 if edut00<200000;
replace schoolstart=1986-9 if edut00>=200000;
gen predschoolstart=1970+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co70';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co70', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1970;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1987.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1987-7 if edut00<200000;
replace schoolstart=1987-9 if edut00>=200000;
gen predschoolstart=1970+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co70';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co70', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1970;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1988.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1988-7 if edut00<200000;
replace schoolstart=1988-9 if edut00>=200000;
gen predschoolstart=1970+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co70';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co70', replace;

*****************************************************************;
*1971 COHORT;
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1971;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1985.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1985-7 if edut00<200000;
replace schoolstart=1985-9 if edut00>=200000;
gen predschoolstart=1971+7;
tab predschoolstart;
tab bmonth;
tempfile co71;
sort pid;
save `co71', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1971;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1986.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1986-7 if edut00<200000;
replace schoolstart=1986-9 if edut00>=200000;
gen predschoolstart=1971+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co71';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co71', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1971;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1987.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1987-7 if edut00<200000;
replace schoolstart=1987-9 if edut00>=200000;
gen predschoolstart=1971+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co71';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co71', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1971;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1988.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1988-7 if edut00<200000;
replace schoolstart=1988-9 if edut00>=200000;
gen predschoolstart=1971+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co71';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co71', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1971;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1989.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1989-7 if edut00<200000;
replace schoolstart=1989-9 if edut00>=200000;
gen predschoolstart=1971+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co71';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co71', replace;
*****************************************************************;
*1972 COHORT
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1972;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1986.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1986-7 if edut00<200000;
replace schoolstart=1986-9 if edut00>=200000;
gen predschoolstart=1972+7;
tab predschoolstart;
tab bmonth;
tempfile co72;
sort pid;
save `co72', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1972;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1987.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1987-7 if edut00<200000;
replace schoolstart=1987-9 if edut00>=200000;
gen predschoolstart=1972+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co72';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co72', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1972;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1988.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1988-7 if edut00<200000;
replace schoolstart=1988-9 if edut00>=200000;
gen predschoolstart=1972+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co72';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co72', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1972;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1989.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1989-7 if edut00<200000;
replace schoolstart=1989-9 if edut00>=200000;
gen predschoolstart=1972+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co72';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co72', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1972;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1990.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1990-7 if edut00<200000;
replace schoolstart=1990-9 if edut00>=200000;
gen predschoolstart=1972+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co72';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co72', replace;



*****************************************************************;
*1973 COHORT
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1973;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1987.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1987-7 if edut00<200000;
replace schoolstart=1987-9 if edut00>=200000;
gen predschoolstart=1973+7;
tab predschoolstart;
tab bmonth;
tempfile co73;
sort pid;
save `co73', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1973;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1988.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1988-7 if edut00<200000;
replace schoolstart=1988-9 if edut00>=200000;
gen predschoolstart=1973+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co73';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co73', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1973;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1989.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1989-7 if edut00<200000;
replace schoolstart=1989-9 if edut00>=200000;
gen predschoolstart=1973+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co73';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co73', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1973;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1990.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1990-7 if edut00<200000;
replace schoolstart=1990-9 if edut00>=200000;
gen predschoolstart=1973+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co73';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co73', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1973;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1991.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1991-7 if edut00<200000;
replace schoolstart=1991-9 if edut00>=200000;
gen predschoolstart=1973+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co73';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co73', replace;




*****************************************************************;
*1974 COHORT
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1974;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1988.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1988-7 if edut00<200000;
replace schoolstart=1988-9 if edut00>=200000;
gen predschoolstart=1974+7;
tab predschoolstart;
tab bmonth;
tempfile co74;
sort pid;
save `co74', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1974;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1989.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1989-7 if edut00<200000;
replace schoolstart=1989-9 if edut00>=200000;
gen predschoolstart=1974+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co74';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co74', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1974;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1990.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1990-7 if edut00<200000;
replace schoolstart=1990-9 if edut00>=200000;
gen predschoolstart=1974+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co74';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co74', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1974;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1991.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1991-7 if edut00<200000;
replace schoolstart=1991-9 if edut00>=200000;
gen predschoolstart=1974+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co74';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co74', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1974;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1992.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1992-7 if edut00<200000;
replace schoolstart=1992-9 if edut00>=200000;
gen predschoolstart=1974+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co74';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co74', replace;


*****************************************************************;
*1975 COHORT
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1975;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1989.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1989-7 if edut00<200000;
replace schoolstart=1989-9 if edut00>=200000;
gen predschoolstart=1975+7;
tab predschoolstart;
tab bmonth;
tempfile co75;
sort pid;
save `co75', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1975;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1990.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1990-7 if edut00<200000;
replace schoolstart=1990-9 if edut00>=200000;
gen predschoolstart=1975+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co75';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co75', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1975;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1991.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1991-7 if edut00<200000;
replace schoolstart=1991-9 if edut00>=200000;
gen predschoolstart=1975+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co75';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co75', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1975;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1992.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1992-7 if edut00<200000;
replace schoolstart=1992-9 if edut00>=200000;
gen predschoolstart=1975+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co75';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co75', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1975;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1993.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1993-7 if edut00<200000;
replace schoolstart=1993-9 if edut00>=200000;
gen predschoolstart=1975+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co75';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co75', replace;

******************************************;
* COHORT 1976*****************************;
******************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1976;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1990.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1990-7 if edut00<200000;
replace schoolstart=1990-9 if edut00>=200000;
gen predschoolstart=1976+7;
tab predschoolstart;
tab bmonth;
tempfile co76;
sort pid;
save `co76', replace;


use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1976;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1991.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1991-7 if edut00<200000;
replace schoolstart=1991-9 if edut00>=200000;
gen predschoolstart=1976+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co76';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co76', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1976;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1992.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1992-7 if edut00<200000;
replace schoolstart=1992-9 if edut00>=200000;
gen predschoolstart=1976+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co76';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co76', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1976;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1993.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1993-7 if edut00<200000;
replace schoolstart=1993-9 if edut00>=200000;
gen predschoolstart=1976+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co76';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co76', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1976;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1994.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1994-7 if edut00<200000;
replace schoolstart=1994-9 if edut00>=200000;
gen predschoolstart=1976+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co76';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co76', replace;

*****************************************************************;
*1977 COHORT
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1977;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1991.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1991-7 if edut00<200000;
replace schoolstart=1991-9 if edut00>=200000;
gen predschoolstart=1977+7;
tab predschoolstart;
tab bmonth;
tempfile co77;
sort pid;
save `co77', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1977;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1992.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1992-7 if edut00<200000;
replace schoolstart=1992-9 if edut00>=200000;
gen predschoolstart=1977+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co77';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co77', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1977;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1993.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1993-7 if edut00<200000;
replace schoolstart=1993-9 if edut00>=200000;
gen predschoolstart=1977+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co77';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co77', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1977;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1994.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1994-7 if edut00<200000;
replace schoolstart=1994-9 if edut00>=200000;
gen predschoolstart=1977+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co77';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co77', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1977;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1995.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1995-7 if edut00<200000;
replace schoolstart=1995-9 if edut00>=200000;
gen predschoolstart=1977+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co77';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co77', replace;

*****************************************************************;
*1978 COHORT
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1978;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1992.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1992-7 if edut00<200000;
replace schoolstart=1992-9 if edut00>=200000;
gen predschoolstart=1978+7;
tab predschoolstart;
tab bmonth;
tempfile co78;
sort pid;
save `co78', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1978;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1993.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1993-7 if edut00<200000;
replace schoolstart=1993-9 if edut00>=200000;
gen predschoolstart=1978+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co78';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co78', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1978;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1994.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1994-7 if edut00<200000;
replace schoolstart=1994-9 if edut00>=200000;
gen predschoolstart=1978+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co78';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co78', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1978;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1995.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1995-7 if edut00<200000;
replace schoolstart=1995-9 if edut00>=200000;
gen predschoolstart=1978+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co78';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co78', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1978;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1996.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1996-7 if edut00<200000;
replace schoolstart=1996-9 if edut00>=200000;
gen predschoolstart=1978+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co78';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co78', replace;
*****************************************************************;
*1979 COHORT
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1979;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1993.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1993-7 if edut00<200000;
replace schoolstart=1993-9 if edut00>=200000;
gen predschoolstart=1979+7;
tab predschoolstart;
tab bmonth;
tempfile co79;
sort pid;
save `co79', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1979;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1994.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1994-7 if edut00<200000;
replace schoolstart=1994-9 if edut00>=200000;
gen predschoolstart=1979+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co79';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co79', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1979;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1995.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1995-7 if edut00<200000;
replace schoolstart=1995-9 if edut00>=200000;
gen predschoolstart=1979+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co79';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co79', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1979;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1996.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1996-7 if edut00<200000;
replace schoolstart=1996-9 if edut00>=200000;
gen predschoolstart=1979+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co79';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co79', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1979;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1997.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1997-7 if edut00<200000;
replace schoolstart=1997-9 if edut00>=200000;
gen predschoolstart=1979+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co79';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co79', replace;
*****************************************************************;
*1980 COHORT;
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1980;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1994.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1994-7 if edut00<200000;
replace schoolstart=1994-9 if edut00>=200000;
gen predschoolstart=1980+7;
tab predschoolstart;
tab bmonth;
tempfile co80;
sort pid;
save `co80', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1980;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1995.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1995-7 if edut00<200000;
replace schoolstart=1995-9 if edut00>=200000;
gen predschoolstart=1980+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co80';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co80', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1980;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1996.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1996-7 if edut00<200000;
replace schoolstart=1996-9 if edut00>=200000;
gen predschoolstart=1980+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co80';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co80', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1980;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1997.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1997-7 if edut00<200000;
replace schoolstart=1997-9 if edut00>=200000;
gen predschoolstart=1980+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co80';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co80', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1980;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1998.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1998-7 if edut00<200000;
replace schoolstart=1998-9 if edut00>=200000;
gen predschoolstart=1980+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co80';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co80', replace;
*****************************************************************;
*1981 COHORT;
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1981;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1995.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1995-7 if edut00<200000;
replace schoolstart=1995-9 if edut00>=200000;
gen predschoolstart=1981+7;
tab predschoolstart;
tab bmonth;
tempfile co81;
sort pid;
save `co81', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1981;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1996.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1996-7 if edut00<200000;
replace schoolstart=1996-9 if edut00>=200000;
gen predschoolstart=1981+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co81';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co81', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1981;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1997.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1997-7 if edut00<200000;
replace schoolstart=1997-9 if edut00>=200000;
gen predschoolstart=1981+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co81';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co81', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1981;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1998.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1998-7 if edut00<200000;
replace schoolstart=1998-9 if edut00>=200000;
gen predschoolstart=1981+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co81';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co81', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1981;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1999.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1999-7 if edut00<200000;
replace schoolstart=1999-9 if edut00>=200000;
gen predschoolstart=1981+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co81';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co81', replace;
*****************************************************************;
*1982 COHORT
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1982;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1996.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1996-7 if edut00<200000;
replace schoolstart=1996-9 if edut00>=200000;
gen predschoolstart=1982+7;
tab predschoolstart;
tab bmonth;
tempfile co82;
sort pid;
save `co82', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1982;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1997.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1997-7 if edut00<200000;
replace schoolstart=1997-9 if edut00>=200000;
gen predschoolstart=1982+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co82';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co82', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1982;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1998.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1998-7 if edut00<200000;
replace schoolstart=1998-9 if edut00>=200000;
gen predschoolstart=1982+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co82';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co82', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1982;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1999.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1999-7 if edut00<200000;
replace schoolstart=1999-9 if edut00>=200000;
gen predschoolstart=1982+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co82';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co82', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1982;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2000.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2000-7 if edut00<200000;
replace schoolstart=2000-9 if edut00>=200000;
gen predschoolstart=1982+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co82';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co82', replace;



*****************************************************************;
*1983 COHORT
*****************************************************************;


use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1983;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1997.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1997-7 if edut00<200000;
replace schoolstart=1997-9 if edut00>=200000;
gen predschoolstart=1983+7;
tab predschoolstart;
tab bmonth;
tempfile co83;
sort pid;
save `co83', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1983;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1998.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1998-7 if edut00<200000;
replace schoolstart=1998-9 if edut00>=200000;
gen predschoolstart=1983+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co83';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co83', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1983;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1999.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1999-7 if edut00<200000;
replace schoolstart=1999-9 if edut00>=200000;
gen predschoolstart=1983+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co83';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co83', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1983;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2000.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2000-7 if edut00<200000;
replace schoolstart=2000-9 if edut00>=200000;
gen predschoolstart=1983+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co83';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co83', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1983;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2001.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2001-7 if edut00<200000;
replace schoolstart=2001-9 if edut00>=200000;
gen predschoolstart=1983+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co83';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co83', replace;



*****************************************************************;
*1984 COHORT
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1984;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1998.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1998-7 if edut00<200000;
replace schoolstart=1998-9 if edut00>=200000;
gen predschoolstart=1984+7;
tab predschoolstart;
tab bmonth;
tempfile co84;
sort pid;
save `co84', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1984;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1999.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1999-7 if edut00<200000;
replace schoolstart=1999-9 if edut00>=200000;
gen predschoolstart=1984+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co84';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co84', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1984;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2000.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2000-7 if edut00<200000;
replace schoolstart=2000-9 if edut00>=200000;
gen predschoolstart=1984+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co84';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co84', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1984;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2001.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2001-7 if edut00<200000;
replace schoolstart=2001-9 if edut00>=200000;
gen predschoolstart=1984+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co84';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co84', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1984;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2002.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2002-7 if edut00<200000;
replace schoolstart=2002-9 if edut00>=200000;
gen predschoolstart=1984+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co84';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co84', replace;


*****************************************************************;
*1985 COHORT
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1985;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_1999.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=1999-7 if edut00<200000;
replace schoolstart=1999-9 if edut00>=200000;
gen predschoolstart=1985+7;
tab predschoolstart;
tab bmonth;
tempfile co85;
sort pid;
save `co85', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1985;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2000.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2000-7 if edut00<200000;
replace schoolstart=2000-9 if edut00>=200000;
gen predschoolstart=1985+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co85';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co85', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1985;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2001.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2001-7 if edut00<200000;
replace schoolstart=2001-9 if edut00>=200000;
gen predschoolstart=1985+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co85';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co85', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1985;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2002.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2002-7 if edut00<200000;
replace schoolstart=2002-9 if edut00>=200000;
gen predschoolstart=1985+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co85';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co85', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1985;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2003.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2003-7 if edut00<200000;
replace schoolstart=2003-9 if edut00>=200000;
gen predschoolstart=1985+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co85';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co85', replace;



******************************************;
* COHORT 1986*****************************;
******************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1986;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2000.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2000-7 if edut00<200000;
replace schoolstart=2000-9 if edut00>=200000;
gen predschoolstart=1986+7;
tab predschoolstart;
tab bmonth;
tempfile co86;
sort pid;
save `co86', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1986;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2001.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2001-7 if edut00<200000;
replace schoolstart=2001-9 if edut00>=200000;
gen predschoolstart=1986+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co86';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co86', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1986;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2002.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2002-7 if edut00<200000;
replace schoolstart=2002-9 if edut00>=200000;
gen predschoolstart=1986+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co86';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co86', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1986;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2003.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2003-7 if edut00<200000;
replace schoolstart=2003-9 if edut00>=200000;
gen predschoolstart=1986+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co86';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co86', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1986;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2004.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2004-7 if edut00<200000;
replace schoolstart=2004-9 if edut00>=200000;
gen predschoolstart=1986+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co86';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co86', replace;


*****************************************************************;
*1987 COHORT;
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1987;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2001.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2001-7 if edut00<200000;
replace schoolstart=2001-9 if edut00>=200000;
gen predschoolstart=1987+7;
tab predschoolstart;
tab bmonth;
tempfile co87;
sort pid;
save `co87', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1987;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2002.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2002-7 if edut00<200000;
replace schoolstart=2002-9 if edut00>=200000;
gen predschoolstart=1987+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co87';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co87', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1987;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2003.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2003-7 if edut00<200000;
replace schoolstart=2003-9 if edut00>=200000;
gen predschoolstart=1987+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co87';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co87', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1987;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2004.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2004-7 if edut00<200000;
replace schoolstart=2004-9 if edut00>=200000;
gen predschoolstart=1987+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co87';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co87', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1987;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2005.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2005-7 if edut00<200000;
replace schoolstart=2005-9 if edut00>=200000;
gen predschoolstart=1987+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co87';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co87', replace;

*****************************************************************;
*1988 COHORT;
*****************************************************************;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1988;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2002.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2002-7 if edut00<200000;
replace schoolstart=2002-9 if edut00>=200000;
gen predschoolstart=1988+7;
tab predschoolstart;
tab bmonth;
tempfile co88;
sort pid;
save `co88', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1988;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2003.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2003-7 if edut00<200000;
replace schoolstart=2003-9 if edut00>=200000;
gen predschoolstart=1988+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co88';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co88', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1988;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2004.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2004-7 if edut00<200000;
replace schoolstart=2004-9 if edut00>=200000;
gen predschoolstart=1988+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co88';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
save `co88', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1988;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2005.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2005-7 if edut00<200000;
replace schoolstart=2005-9 if edut00>=200000;
gen predschoolstart=1988+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co88';
tab _merge;
keep if _merge==1 |_merge==2;
drop _merge;
sort pid;
save `co88', replace;

use pid foedselsaar bmonth cob2006 using /data/regdta/pop_1986g2006;
rename foedselsaar yob;
keep if yob==1988;
keep if cob2006==000;
sum;
tab bmonth;
sort pid;
duplicates report;
duplicates drop;
merge pid using /data/utddta/finished_edu_2006.dta;
tab _merge;
keep if _merge==3;
drop _merge;
sum;
tab edut00;
tab utd;
tab utfall;
tab avgyr;
tab avgyr utd;
keep if edut00<300000;
gen schoolstart=2006-7 if edut00<200000;
replace schoolstart=2006-9 if edut00>=200000;
gen predschoolstart=1988+7;
tab bmonth;
tab predschoolstart;
sort pid;
merge pid using `co88';
tab _merge;
keep if _merge==1 | _merge==2;
drop _merge;
sort pid;
tab schoolstart;
tab predschoolstart;
save `co88', replace;

append using `co62';
append using `co63';
append using `co64';
append using `co65';
append using `co66';
append using `co67';
append using `co68';
append using `co69';
append using `co70';

append using `co71';
append using `co72';
append using `co73';
append using `co74';
append using `co75';
append using `co76';
append using `co77';
append using `co78';
append using `co79';
append using `co80';

append using `co81';
append using `co82';
append using `co83';
append using `co84';
append using `co85';
append using `co86';
append using `co87';

bysort yob: tab schoolstart;
bysort yob: tab predschoolstart;

sort pid;
save "/home0/kjell/schoolstart/dta/realschoolstart.dta", replace;

use /home0/kjell/schoolstart/dta/realschoolstart.dta;
sort pid;
duplicates report pid;
duplicates drop pid, force;
sort pid;

save /home/sblack/school_age/realschoolstart.dta, replace;


******PART 4 -- MERGING THE DATASETS********************************;

use ~/school_age/restat/child_data;
duplicates report pid;
duplicates drop pid, force;
sort pid;
merge pid using ~/school_age/realschoolstart;
tab _merge;
keep if _merge==3 | _merge==1;
drop _merge;
sort pid;
merge pid using ~/school_age/restat/parent_data;
tab _merge;
keep if _merge==3 | _merge==1;
drop _merge;
su;

save ~/school_age/restat/ssa_data, replace;

